package com.ruoyi.quartz.task;

import cn.hutool.http.HttpUtil;
import com.ruoyi.quartz.domain.WechatyInfo;
import com.ruoyi.quartz.service.IWechatyInfoService;
import com.ruoyi.quartz.vo.WechatyMessageVo;
import com.snowRemovingUser.domain.TPrediction;
import com.snowRemovingUser.domain.TRoadManage;
import com.snowRemovingUser.service.ITPredictionService;
import com.snowRemovingUser.service.ITRoadManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component("wechatyBotTask")
public class WechatyBotTask {
    @Autowired
    private IWechatyInfoService wechatyInfoService;
    @Autowired
    private ITPredictionService predictionService;
    @Autowired
    private ITRoadManageService roadManageService;

    public static final String WECHATY_ROOM_PIC = "http://42.192.119.47:13000/sendPicToRoom";
    public static final String WECHATY_ROOM_MSG = "http://42.192.119.47:13000/sendToRoom";
    public static final String WECHATY_PERSONAL_MSG = "http://42.192.119.47:13000/send";
    public static final int TIMEOUT = 5000;

    public void sendPicToRoom(String room,String picPath) {
        StringBuilder sendText= new StringBuilder();
        TPrediction predictionQuary=new TPrediction();
        predictionQuary.setParentId(0L);
        List<TPrediction> predictionList=predictionService.selectTPredictionList(predictionQuary);
        if(predictionList.isEmpty()){
            log.error("预测报表为空");
        }
        List<TRoadManage> roadList=roadManageService.selectTRoadManageList(new TRoadManage());
        Map<String,String> roadMap=roadList.stream().collect(Collectors.toMap(TRoadManage::getPlace,TRoadManage::getPlaceName));


        Date date = predictionList.get(0).getLocalTime();
        SimpleDateFormat sdf = new SimpleDateFormat("M月d日 HH:mm");
        String formattedDate = sdf.format(date);

        sendText.append("发布时间：").append(formattedDate).append("\n");
        for(TPrediction prediction:predictionList){
            sendText.append(roadMap.get(prediction.getPlaceName())).append("：").append(prediction.getComment()).append("\n");
        }
        sendText.delete(sendText.length()-2,sendText.length());
        WechatyMessageVo picVo = new WechatyMessageVo(room, picPath);
        WechatyMessageVo msgVo=new WechatyMessageVo(room,sendText.toString());
        try {
            HttpUtil.post(WECHATY_ROOM_MSG, msgVo.toJsonStr(), TIMEOUT);
            HttpUtil.post(WECHATY_ROOM_PIC, picVo.toJsonStr(), TIMEOUT);
        } catch (Exception e) {
            log.error("消息推送失败", e);
        }
    }

    public void sendToRoom(String room,String message) {
        WechatyMessageVo wechatyMessageVo = new WechatyMessageVo(room, message);
        try {
            HttpUtil.post(WECHATY_ROOM_MSG, wechatyMessageVo.toJsonStr(), TIMEOUT);
        } catch (Exception e) {
            log.error("消息推送失败", e);
        }
    }

    public void send(String userId,String message) {
        WechatyMessageVo wechatyMessageVo = new WechatyMessageVo(userId, message);
        try {
            HttpUtil.post(WECHATY_PERSONAL_MSG, wechatyMessageVo.toJsonStr(), TIMEOUT);
        } catch (Exception e) {
            log.error("消息推送失败", e);
        }
    }

    public void heartBeat() {
        try {
            String result=HttpUtil.get("http://localhost:13000/getStatus", TIMEOUT);
            log.info("心跳检测结果:{}", result);
            WechatyInfo wechatyInfo=wechatyInfoService.selectWechatyInfoById(1);
            wechatyInfo.setIsLogin(result);
            wechatyInfo.setUpdateTime(new Date());
            wechatyInfoService.updateWechatyInfo(wechatyInfo);
        } catch (Exception e) {
            log.error("心跳检测失败", e);
        }
    }

    public void restart(){
        try {
            String result=HttpUtil.get("http://localhost:13000/restart", TIMEOUT);
            log.info("机器人重启结果:{}", result);
        } catch (Exception e){
            log.error("机器人重启失败", e);
        }
    }
}
